import React from 'react';
import PropTypes from 'prop-types';
import {Card, Button, Row, Col} from 'antd';
import {DATE_FORMAT} from '../constants';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import {PERMISSIONS} from './constants';
import {hasPermissions} from './utils';
import routes from './routes';
import {injectIntl} from './intl';
import {lcKind, lcStatus, currency} from '../Enum';
import {conventEnumValueToString, formatDateTime} from '../utils';
import styles from './style.css';
import {getDetail, closeOrder, approveOrder, submitOrder, onSubmitReject, onSubmitPass} from './api';
const {Description} = DescriptionList;
export class FinanceApprove extends React.PureComponent {
    state = {
        loading: false,
        data: {},
    }
    componentDidMount() {
        this.getDetailInfo();
    }
    getDetailInfo = () => {
        const id = this.props.id;
        this.setState({loading: true});
        getDetail(id)
            .then(res => {
                if(res.ok)
                    this.setState({
                        loading: false,
                        data: res.data,
                        details: res.data.details,
                        usedDetails: res.data.usedDetails
                    });
                else
                    this.setState(({
                        loading: false
                    }));
            });
    }
    handleUpdateClick = () => {
        this.props.history.push(routes.update.format(this.props.id));
    }
    handleReceiveClick = () => {
        this.props.history.push(routes.receive.format(this.props.id));
    }
    onClickCloseBtn = () => {
        this.setState({loading: true});
        closeOrder(this.state.data.id, this.state.data.rowVersion)
            .then(res => {
                this.setState({loading: false});
                if(res.ok)
                    this.props.successReturn();
            });
    }
    onClickApproveBtn = () => {
        this.setState({loading: true});
        approveOrder(this.state.data.id, this.state.data.rowVersion)
            .then(res => {
                this.setState({loading: false});
                if(res.ok)
                    this.props.successReturn();
            });
    }
    onClickSubmitBtn = () => {
        this.setState({loading: true});
        submitOrder(this.state.data.id, this.state.data.rowVersion)
            .then(res => {
                this.setState({loading: false});
                if(res.ok)
                    this.props.successReturn();
            });
    }
    onSubmitReject = () => {
        this.setState({loading: true});
        onSubmitReject(this.state.data.id, this.state.data.rowVersion).then(res => {
            this.setState({loading: false});
            if(res.ok)
                this.props.successReturn();
        });
    }
    onSubmitPass = () => {
        this.setState({loading: true});
        onSubmitPass(this.state.data.id, this.state.data.rowVersion).then(res => {
            this.setState({loading: false});
            if(res.ok)
                this.props.successReturn();
        });
    }
    render() {
        const {formatMessage} = this.props.intl;
        const {data} = this.state;
        const Yes = formatMessage({
            id: 'detailPage.yes',
            defaultMessage: '是'
        });
        const No = formatMessage({
            id: 'detailPage.no',
            defaultMessage: '否'
        });
        const submitable = !hasPermissions(PERMISSIONS.financeApprove, this.props.permissions);
        return (
            <div className="detail">
                <Card loading={this.state.loading}>
                    <DescriptionList size="large" title={<Row><Col span={6}>{formatMessage({
                        id: 'detailPanel.text.title',
                        defaultMessage: '信用证信息'
                    })}</Col>
                    </Row>}>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.branchCode',
                            defaultMessage: '营销分公司编号'
                        })}>{data.branchCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.branchName',
                            defaultMessage: '营销分公司名称'
                        })}>{data.dealerName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.brandCode',
                            defaultMessage: '品牌编号'
                        })}>{data.brandCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.brandName',
                            defaultMessage: '品牌名称'
                        })}>{data.brandName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.dealerCode',
                            defaultMessage: '总代编号'
                        })}>{data.dealerCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.dealerName',
                            defaultMessage: '总代名称'
                        })}>{data.dealerName}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.lcCode',
                            defaultMessage: '信用证号码'
                        })}>{data.lcCode}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.issueCountry',
                            defaultMessage: '开证国家'
                        })}>{data.issueCountry}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.issueBank',
                            defaultMessage: '开证银行'
                        })}>{data.issueBank}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.issuePrice',
                            defaultMessage: '开证金额'
                        })}>{data.issuePrice}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.currency',
                            defaultMessage: '币种'
                        })}>{conventEnumValueToString(currency, data.currency)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.lcPayMentDays',
                            defaultMessage: '信用证账期'
                        })}>{data.lcPayMentDays}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.issueDate',
                            defaultMessage: '开证日期'
                        })}>{formatDateTime(data.issueDate, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.lastLoadDate',
                            defaultMessage: '最迟装船日期'
                        })}>{formatDateTime(data.lastLoadDate, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.isAllowBatch',
                            defaultMessage: '是否允许分批'
                        })}>{data.isAllowBatch ? Yes : No}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.isAllowTransfer',
                            defaultMessage: '是否允许转运'
                        })}>{data.isAllowTransfer ? Yes : No}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.quantity',
                            defaultMessage: '件数'
                        })}>{data.quantity}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.effectiveDate',
                            defaultMessage: '生效日期'
                        })}>{formatDateTime(data.effectiveDate, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.expirationDate',
                            defaultMessage: '失效日期'
                        })}>{formatDateTime(data.expirationDate, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.lcKind',
                            defaultMessage: '信用证类型'
                        })}>{conventEnumValueToString(lcKind, data.lcKind)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.lcStatus',
                            defaultMessage: '信用证状态'
                        })}>{conventEnumValueToString(lcStatus, data.lcStatus)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.text.remark',
                            defaultMessage: '备注'
                        })}>{data.remark}</Description>
                    </DescriptionList>
                </Card>
                <Card className={styles.optionBtn}>
                    <Button className={styles.marginBtn}
                        onClick={this.onSubmitReject}
                        disabled={!submitable}
                        loading={this.state.loading}>
                        {formatMessage({
                            id: 'approvePanel.operation.onSubmitReject',
                            defaultMessage: '审核不通过'
                        })}
                    </Button>
                    <Button
                        type="primary"
                        onClick={this.onSubmitPass}
                        disabled={!submitable}
                        loading={this.state.loading}>
                        {formatMessage({
                            id: 'approvePanel.operation.onSubmitPass',
                            defaultMessage: '审核通过'
                        })}
                    </Button>
                </Card>
                <div className="page-toolbar">
                    <Button
                        type="primary"
                        disabled={this.state.loading}
                        onClick={this.props.onCancel}>{formatMessage({
                            id: 'detailPage.text.return',
                            defaultMessage: '返回'
                        })}</Button>
                </div>
            </div>
        );
    }
}

FinanceApprove.propTypes = {
    id: PropTypes.string.isRequired,
    permissions: PropTypes.array.isRequired,
    onCancel: PropTypes.func.isRequired,
    history: PropTypes.object,
    intl: PropTypes.object,
    successReturn: PropTypes.func,
};

import {connect} from 'react-redux';
import {
    tableSearch
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);


const mapStateToProps = (state, props) => ({
    id: props.id,
    permissions: getPermissions(state),
});
const mapDispatchToProps = (dispatch, props) => ({
    handleReceiveClick: () => {
        props.history.push(routes.receive.format(props.id));
    },
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
    onCancel: () => {
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(FinanceApprove));
